package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.QueryInfo;
import com.example.jyjava.bean.RoleQueryInfo;
import com.example.jyjava.bean.ShopQueryInfo;
import com.example.jyjava.bean.User;
import com.example.jyjava.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDao userDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private CollectDao collectDao;
    @Autowired
    private TabDao tabDao;

    @RequestMapping("/getId")
    public String getId(@RequestBody User user){
        String falg = "400";;
        int i = userDao.updateId(user.getUsername(), user.getEmail(), user.getUserUrl(), user.getId());
        User ur = userDao.getId(user.getId());
        HashMap<String, Object> res = new HashMap<>();  //map封装

        if (ur != null){
            falg = "200";
            res.put("flag",falg);
            res.put("user",ur);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/allUser")
    public String getUserList(RoleQueryInfo roleQueryInfo){
        String falg = "400";
        //获取最大列表和当前编号
        int numbers = userDao.getUserCounts("%" + roleQueryInfo.getQuery() + "%",roleQueryInfo.getRole());
        int pageStart = (roleQueryInfo.getPageNum()-1)*roleQueryInfo.getPageSize();
        List<User> users = userDao.getAllUser("%" + roleQueryInfo.getQuery() + "%", pageStart, roleQueryInfo.getPageSize(),roleQueryInfo.getRole());
        HashMap<String, Object> res = new HashMap<>();

        System.out.println(numbers);
        if (users != null){
            falg = "200";
            res.put("flag",falg);
            res.put("number",numbers);
            res.put("data",users);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){
        String falg = "400";
        user.setState(true);
        user.setRole("3");
        if (user.getUserUrl() == ""){
            user.setUserUrl("https://lwzeng.oss-cn-beijing.aliyuncs.com/ce8dbd6515e3444ebde491928318e3f8QIdh3ddaxIXB8cc8c68c56c826787a12afe948d59319.jpg");
        }
        int i = userDao.addUser(user);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("user",i);
            res.put("msg","'注册成功'");
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/userState")
    public String updateUserState(@RequestParam Integer id,
                                  @RequestParam Boolean state){
        String falg = "400";
        int i = userDao.updateState(id,state);
        HashMap<String, Object> res = new HashMap<>();

        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("user",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/getUpdateUser")
    public String getUpdateUser(int id){
        String falg = "400";
        User user = userDao.getUpdateUser(id);
        HashMap<String, Object> res = new HashMap<>();

        if (user != null){
            falg = "200";
            res.put("flag",falg);
            res.put("user",user);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/editUser")
    public String editUser(@RequestBody User user){
        String falg = "400";
        int i = userDao.editUser(user);
        HashMap<String, Object> res = new HashMap<>();

        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("user",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        String falg = "400";
        int i = userDao.deleteUser(id);
        HashMap<String, Object> res = new HashMap<>();

        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("user",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/editRole")
    public String editRole(@RequestBody User user){
        String falg = "400";
        int i = userDao.editRole(user.getId(),user.getRole());
        HashMap<String, Object> res = new HashMap<>();

        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("user",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/num")
    public String num(@RequestBody ShopQueryInfo shopQueryInfo){
        String falg = "400";
        int a = shopDao.getShopNum(shopQueryInfo.getUserId());
        int b = collectDao.getCollectC(shopQueryInfo.getQuery(),shopQueryInfo.getUserId());
        int c = orderDao.getOrderC(shopQueryInfo.getQuery(),shopQueryInfo.getUserId(),null);
        int d = orderDao.getOrderC(shopQueryInfo.getQuery(),null,shopQueryInfo.getSellerId());
        HashMap<String, Object> res = new HashMap<>();

        if (a > -1){
            falg = "200";
            res.put("flag",falg);
            res.put("shop",a);
            res.put("collect",b);
            res.put("order",c);
            res.put("seOrder",d);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/num2")
    public String num2(){
        String falg = "400";
        int a = userDao.getUserCounts(null,null);
        int b = tabDao.getTabCounts(null);
        HashMap<String, Object> res = new HashMap<>();

        if (a > -1){
            falg = "200";
            res.put("flag",falg);
            res.put("user",a);
            res.put("tab",b);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/num3")
    public String num3(){
        String falg = "400";
        int a = shopDao.getShopNum(null);
        int b = collectDao.getCollectC(null,null);
        int c = orderDao.getOrderC(null,null,null);
        HashMap<String, Object> res = new HashMap<>();

        if (a > -1){
            falg = "200";
            res.put("flag",falg);
            res.put("shop",a);
            res.put("collect",b);
            res.put("order",c);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }
}
